package com.lanren.androidanimations;

import android.animation.Animator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.lanren.androidanimations.library.TechniquesHelper;
import com.lanren.androidanimations.library.YoYo;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity {

    private ListView mListView;
    private EffectAdapter mAdapter;
    private View mTarget;
    private YoYo.YoYoString rope;

    private List<AnimaBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        setData();
        mListView = (ListView) findViewById(R.id.list_items);
        mTarget = findViewById(R.id.hello_world);

        mAdapter = new EffectAdapter(this, list);
        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (rope != null) {
                    rope.stop(true);
                }
                rope = YoYo.with(list.get(position).getAnima())
                        .duration(1200)
                        .repeat(YoYo.INFINITE)
                        .pivot(YoYo.CENTER_PIVOT, YoYo.CENTER_PIVOT)
                        .interpolate(new AccelerateDecelerateInterpolator())
                        .withListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {
                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {
                                Toast.makeText(MyActivity.this, "canceled previous animation", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {

                            }
                        })
                        .playOn(mTarget);
            }
        });
        findViewById(R.id.hello_world).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rope != null) {
                    rope.stop(true);
                }
            }
        });
    }

    private void setData() {
        if (list.size() > 0) {
            list.clear();
        }
        list.add(new AnimaBean(TechniquesHelper.DropOut, "DropOut"));
        list.add(new AnimaBean(TechniquesHelper.Landing, "Landing"));
        list.add(new AnimaBean(TechniquesHelper.TakingOff, "TakingOff"));
        list.add(new AnimaBean(TechniquesHelper.Flash, "Flash"));
        list.add(new AnimaBean(TechniquesHelper.Pulse, "Pulse"));
        list.add(new AnimaBean(TechniquesHelper.RubberBand, "RubberBand"));
        list.add(new AnimaBean(TechniquesHelper.Shake, "Shake"));
        list.add(new AnimaBean(TechniquesHelper.Swing, "Swing"));
        list.add(new AnimaBean(TechniquesHelper.Wobble, "Wobble"));
        list.add(new AnimaBean(TechniquesHelper.Bounce, "Bounce"));
        list.add(new AnimaBean(TechniquesHelper.Tada, "Tada"));
        list.add(new AnimaBean(TechniquesHelper.StandUp, "StandUp"));
        list.add(new AnimaBean(TechniquesHelper.Wave, "Wave"));
        list.add(new AnimaBean(TechniquesHelper.Hinge, "Hinge"));
        list.add(new AnimaBean(TechniquesHelper.RollIn, "RollIn"));
        list.add(new AnimaBean(TechniquesHelper.RollOut, "RollOut"));
        list.add(new AnimaBean(TechniquesHelper.BounceIn, "BounceIn"));
        list.add(new AnimaBean(TechniquesHelper.BounceInDown, "BounceInDown"));
        list.add(new AnimaBean(TechniquesHelper.BounceInLeft, "BounceInLeft"));
        list.add(new AnimaBean(TechniquesHelper.BounceInRight, "BounceInRight"));
        list.add(new AnimaBean(TechniquesHelper.BounceInUp, "BounceInUp"));
        list.add(new AnimaBean(TechniquesHelper.FadeIn, "FadeIn"));
        list.add(new AnimaBean(TechniquesHelper.FadeInUp, "FadeInUp"));
        list.add(new AnimaBean(TechniquesHelper.FadeInDown, "FadeInDown"));
        list.add(new AnimaBean(TechniquesHelper.FadeInLeft, "FadeInLeft"));
        list.add(new AnimaBean(TechniquesHelper.FadeInRight, "FadeInRight"));
        list.add(new AnimaBean(TechniquesHelper.FadeOut, "FadeOut"));
        list.add(new AnimaBean(TechniquesHelper.FadeOutDown, "FadeOutDown"));
        list.add(new AnimaBean(TechniquesHelper.FadeOutLeft, "FadeOutLeft"));
        list.add(new AnimaBean(TechniquesHelper.FadeOutRight, "FadeOutRight"));
        list.add(new AnimaBean(TechniquesHelper.FadeOutUp, "FadeOutUp"));
        list.add(new AnimaBean(TechniquesHelper.FlipInX, "FlipInX"));
        list.add(new AnimaBean(TechniquesHelper.FlipOutX, "FlipOutX"));
        list.add(new AnimaBean(TechniquesHelper.FlipInY, "FlipInY"));
        list.add(new AnimaBean(TechniquesHelper.FlipOutY, "FlipOutY"));
        list.add(new AnimaBean(TechniquesHelper.RotateIn, "RotateIn"));
        list.add(new AnimaBean(TechniquesHelper.RotateInDownLeft, "RotateInDownLeft"));
        list.add(new AnimaBean(TechniquesHelper.RotateInDownRight, "RotateInDownRight"));
        list.add(new AnimaBean(TechniquesHelper.RotateInUpLeft, "RotateInUpLeft"));
        list.add(new AnimaBean(TechniquesHelper.RotateInUpRight, "RotateInUpRight"));
        list.add(new AnimaBean(TechniquesHelper.RotateOut, "RotateOut"));
        list.add(new AnimaBean(TechniquesHelper.RotateOutDownLeft, "RotateOutDownLeft"));
        list.add(new AnimaBean(TechniquesHelper.RotateOutDownRight, "RotateOutDownRight"));
        list.add(new AnimaBean(TechniquesHelper.RotateOutUpLeft, "RotateOutUpLeft"));
        list.add(new AnimaBean(TechniquesHelper.RotateOutUpRight, "RotateOutUpRight"));
        list.add(new AnimaBean(TechniquesHelper.SlideInLeft, "SlideInLeft"));
        list.add(new AnimaBean(TechniquesHelper.SlideInRight, "SlideInRight"));
        list.add(new AnimaBean(TechniquesHelper.SlideInUp, "SlideInUp"));
        list.add(new AnimaBean(TechniquesHelper.SlideInDown, "SlideInDown"));
        list.add(new AnimaBean(TechniquesHelper.SlideOutLeft, "SlideOutLeft"));
        list.add(new AnimaBean(TechniquesHelper.SlideOutRight, "SlideOutRight"));
        list.add(new AnimaBean(TechniquesHelper.SlideOutUp, "SlideOutUp"));
        list.add(new AnimaBean(TechniquesHelper.SlideOutDown, "SlideOutDown"));
        list.add(new AnimaBean(TechniquesHelper.ZoomIn, "ZoomIn"));
        list.add(new AnimaBean(TechniquesHelper.ZoomInDown, "ZoomInDown"));
        list.add(new AnimaBean(TechniquesHelper.ZoomInLeft, "ZoomInLeft"));
        list.add(new AnimaBean(TechniquesHelper.ZoomInRight, "ZoomInRight"));
        list.add(new AnimaBean(TechniquesHelper.ZoomInUp, "ZoomInUp"));
        list.add(new AnimaBean(TechniquesHelper.ZoomOut, "ZoomOut"));
        list.add(new AnimaBean(TechniquesHelper.ZoomOutDown, "ZoomOutDown"));
        list.add(new AnimaBean(TechniquesHelper.ZoomOutLeft, "ZoomOutLeft"));
        list.add(new AnimaBean(TechniquesHelper.ZoomOutRight, "ZoomOutRight"));
        list.add(new AnimaBean(TechniquesHelper.ZoomOutUp, "ZoomOutUp"));
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus) {
            rope = YoYo.with(TechniquesHelper.FadeIn).duration(1000).playOn(mTarget);// after start,just click mTarget view, rope is not init
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            startActivity(new Intent(this, ExampleActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
